<?php
	require_once( "inc_systemFunctions.php" );
	page_header( "Wayne Schlegel Inc. | Setup", true, "myflight" );
	
	include( "_setup_adminNavi.php" );
?>

<?php
	if( !empty( $_POST['send'] ) && $_FILES['userfile']['size'] > 0 ) {
		$fileName = $_FILES['userfile']['name'];
		$tmpName  = $_FILES['userfile']['tmp_name'];
		$fileSize = $_FILES['userfile']['size'];
		$fileType = $_FILES['userfile']['type'];
		
		if( empty ( $_POST['isActive'] ) ) $_POST['isActive'] = 0;
		
		$fp      = fopen($tmpName, 'r');
		$content = fread($fp, filesize($tmpName));
		$content = addslashes($content);
		fclose($fp);
		
		if(!get_magic_quotes_gpc())
		{
		    $fileName = addslashes($fileName);
		}	
	
		$res  = mysql_query( "INSERT INTO productImages ( description, bin_data, filename, filesize, filetype, isActive, lastWriteAccess ) VALUES ( '" . $_POST['form_description'] . "', '" . $content . "', '" . $fileName . "', '" . $fileSize . "', '" . $fileType . "', '" . $_POST['isActive'] . "', '" . date( "Y-m-d H:i:s" ) . "' )" );
		$lastId = mysql_insert_id();
		echo "<p class=\"green\">The stored file has the following ID: " . $lastId . "</p>\n";
	}
?>


<h1>add product image</h1>
<form class="adminForm" method="post" action="_setup_addProductImage.php" enctype="multipart/form-data" class="admin">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
    <label for="form_description">description</label>
    <input type="text" name="form_description" id="form_description" placeholder="description" /><br />
    <label for="userfile">file to upload/ store in database</label>
    <input type="file" name="userfile" id="userfile" /><br />
    <label for="isActive">active?</label> <input type="checkbox" name="isActive" value="1" id="isActive" checked="checked" /><br />
    <input type="submit" name="send" value="SAVE" />
</form>

<table class="iTable">
	<tr>
		<th>ID</th>
		<th>description</th>
		<th>image</th>
		<th>filename</th>
		<th>filesize</th>
		<th>filetype</th>
		<th>active?</th>
		<th>delete?</th>
		<th>last write access</th>
		<th>tools</th>
	</tr>
	<?php
		$sql = "SELECT * FROM productImages ORDER BY id DESC";
		$res = mysql_query( $sql );
		while( $row = mysql_fetch_array( $res ) ){
			echo "<tr";
			if( !empty( $lastId ) && $lastId == $row['id'] ) { echo " class=\"newItem\" "; }
			echo ">\n";
			echo "<td>" . $row['id'] . "</td>\n";
			echo "<td>" . $row['description'] . "</td>\n";
			echo "<td><img src =\"getBinaryData.php?blobId=" . $row['id'] . "\" height=\"30\" /></td>\n";
			echo "<td>" . $row['filename'] . "</td>\n";
			echo "<td>" . $row['filesize'] . "</td>\n";
			echo "<td>" . $row['filetype'] . "</td>\n";
			echo "<td>" . iconStatus( $row['isActive'] ) . "</td>\n";
			echo "<td>" . iconStatus( $row['isMarkedForDeletion'] ) . "</td>\n";
			echo "<td>" . $row['lastWriteAccess'] . "</td>\n";
			echo "<td></td>\n";
			echo "</tr>\n";
		}
	?>
	<tr class="moreInformation lastItem">
		<td colspan="10"><a href="#">to upload new files use the form above</a></td>
	</tr>
</table>

<?php
	page_footer();
?>